/**
 * 推广明细
 * @author  fubangfu2015@163.com
 * @data    2020/11/28
 * --------------------------------------
 */
import { postAjax } from '../../../utils/ajax';
import { getBDUserId } from '../../../utils/common';
import { SHOP_LEVELS } from '../../../utils/constant';

Page({
  data : {
    // 0全部，1直推，2团队
    type : 0,
    keyword : '',
    levels : SHOP_LEVELS,
    result : [
      {
        'person' : '莫回頭',
        'username' : '15033300700',
        'promotion' : '2',
        'contract_pic' : 'https://ttjx-1302259601.cos.ap-beijing.myqcloud.com/media/upload/u/go/ugo70e1zcjmsqjd1595415552.png',
        'created_at_datetime' : '2020-09-25 16:24:04',
        'recommend_id' : '3',
        'type' : 1,
        'recommend_name' : '天天精选'
      }
    ]
  },
  onLoad({ type }) {
    this.setData({ type });
  },
  onShow : function () {
    const shopHeader = this.selectComponent('.shop-header');
    shopHeader.getShopInfo();
    this.onSearch();
  },
  // 搜索框输入
  oninput(e) {
    this.setData({ keyword : e.detail.value });
  },
  onNavigate(e) {
    this.setData({ type : e.detail.type });
    this.onSearch();
  },
  // 搜索
  onSearch() {
    postAjax('/manage/sub-list', {
      type : this.data.type,
      bdmin_user_id : getBDUserId()
    }).then(result => {
      const keyword = this.data.keyword.trim();
      // 是否有搜索字段
      const has = !!keyword.length;
      this.setData({
        // 没有搜索字段就全部显示
        result : result.filter(res => (!has || res.person.includes(keyword)))
      });
    });
  }
});
